Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remaining 3 phase electrical attributes #339

Draft
wants to merge 11 commits into
base: dev
Choose a base branch
from

Conversation

abmantis
Copy link
Contributor

@abmantis abmantis commented Dec 24, 2024

Follow up to #324

Related HA PR: home-assistant/core#133969

@TheJulianJES TheJulianJES added the entities Issue or PR about (custom) entities label Jan 11, 2025
Copy link

codecov bot commented Jan 11, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.52%. Comparing base (a833f43) to head (15ff910).

Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #339      +/-   ##
==========================================
+ Coverage   96.51%   96.52%   +0.01%     
==========================================
  Files          61       61              
  Lines        9457     9501      +44     
==========================================
+ Hits         9127     9171      +44     
  Misses        330      330              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@abmantis abmantis marked this pull request as ready for review January 15, 2025 18:57
@@ -126,7 +170,6 @@ class MeasurementType(enum.IntFlag):
ElectricalMeasurement.AttributeDefs.measurement_type.name: True,
ElectricalMeasurement.AttributeDefs.power_divisor.name: True,
ElectricalMeasurement.AttributeDefs.power_multiplier.name: True,
ElectricalMeasurement.AttributeDefs.power_factor.name: True,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure why power_factor was here, but I think reporting needs to be configured for it too

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to ZCL spec, it is not reportable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, apparently we also set up attribute reporting for some attributes that don't apparently don't even support it before this PR...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed reporting for all the ones that are unsupported

Copy link
Contributor

@TheJulianJES TheJulianJES Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the "good thing" is that we already poll all supported attributes at runtime, so that's why power_factor and others work in the first place.

But for the ones that change over time, where attribute reporting can't be set up (according to spec), we should just add them to the ZCL_INIT_ATTRS dict with cache=True, as we don't need to poll them when starting up ZHA (the unsupported_attributes check isn't done there), but we do need to poll them at runtime, which is done through the cluster handler, so there is one big attribute read for multiple attributes.

But we should still move some attributes (where attribute reporting isn't possible) from REPORT_CONFIG to ZCL_INIT_ATTRS.
(EDIT: Wrote this before your comment above 😄)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that we only poll attributes that for which we set up reporting, because we only poll the ones in REPORT_CONFIG, so if we move them to ZCL_INIT_ATTRS they will not be polled, right?

Also, why do we poll the ones that have reporting set up?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you're right. I confused the startup polling doing both and this one only doing the reporting attributes.

We poll the attribute because many smart plugs (mostly Tuya) do not respect/allow setting up attribute reporting on most/all EM cluster attributes.

I do wonder a bit how some of this would work at all then, or at least how it works at the moment (with power_factor and so on), but maybe we've just not had any devices that support it? I wanna check how Z2M deals with some of these devices later. It has a polling configuration per plug/device at least.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either no one complained about power_factor not getting updated (since there maybe not be many devices with it), or the devices report the value even without reporting config 🤷

I reverted the attributes back to REPORT_CONFIG for now.

zha/application/platforms/sensor/__init__.py Outdated Show resolved Hide resolved
zha/application/platforms/sensor/__init__.py Outdated Show resolved Hide resolved
zha/application/platforms/sensor/__init__.py Outdated Show resolved Hide resolved
Comment on lines 121 to 123
ElectricalMeasurement.AttributeDefs.power_factor.name: False,
ElectricalMeasurement.AttributeDefs.power_factor_ph_b.name: False,
ElectricalMeasurement.AttributeDefs.power_factor_ph_c.name: False,
Copy link
Contributor

@TheJulianJES TheJulianJES Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per this comment, we should be able to use True for the cache here.
They are polled using the EM cluster at runtime, which checks for unsupported_attributes, so we can avoid the extra polling during during each startup, which doesn't check for unsupported_attributes (yet).
Even if we eventually add it, we can avoid polling this at startup, since we'll poll it after a few seconds/minute anyway.

EDIT: The polling part is not completely correct: #339 (comment)

@abmantis abmantis requested a review from TheJulianJES January 17, 2025 23:56
@TheJulianJES
Copy link
Contributor

TheJulianJES commented Jan 18, 2025

Instead of trying to set up attribute reporting for attributes that aren't reportable according to spec, just so the EM cluster will poll them with the current implementation, we could also add another ZCL_POLLING_ATTRS list with just the attributes that should be polled and update async_update in the EM cluster handler.

Then, we'd have all attributes that need to be initialized in ZCL_INIT_ATTRS with True. They will be polled later on anyways (in batches of 5), so no need to force poll them individually at startup.
Only attributes that support attribute reporting would be in REPORT_CONFIG.
And all attributes that aren't reportable but which can still update over time would go in ZCL_POLLING_ATTRS.

async_update should then be updated to poll the attributes from ZCL_POLLING_ATTRS, instead of REPORT_CONFIG.
For clarity, I'd also rather duplicate some of the attribute names between ZCL_POLLING_ATTRS and REPORT_CONFIG (instead of async_update combining them and polling those all).


Another thing, do we really need to poll the _max attributes all the time? Do we even display/use them anywhere?

@abmantis
Copy link
Contributor Author

For clarity, I'd also rather duplicate some of the attribute names between ZCL_POLLING_ATTRS and REPORT_CONFIG (instead of async_update combining them and polling those all).

In that case, won't all the attributes in REPORT_CONFIG also be in ZCL_POLLING_ATTRS then, due to the issue you mention of Tuya devices not reporting them?

Another thing, do we really need to poll the _max attributes all the time? Do we even display/use them anywhere?

To be honest I don't know. Since they were already in REPORT_CONFIG, I assumed they were needed and did the same for phases b/c.

@TheJulianJES
Copy link
Contributor

In that case, won't all the attributes in REPORT_CONFIG also be in ZCL_POLLING_ATTRS then, due to the issue you mention of Tuya devices not reporting them?

Yeah, probably all attributes that are in REPORT_CONFIG should also go in ZCL_POLLING_ATTRS for now.
(But not all attributes that are in ZCL_POLLING_ATTRS should go in REPORT_CONFIG.)
Other than separating it for clarity, I could see that the polling attributes change over time, since we might only need to poll some attributes (could peek into Z2M for that). Currently, we just poll all reportable attrs.

If it looks really stupid to duplicate most/all attributes, we can also combine them in async_update for now. Personally, I'd rather have this be "clear" and easily changeable, but if it's bad/overwhelming, we can do it differently.

abmantis added a commit to abmantis/zha that referenced this pull request Jan 19, 2025
As suggested in zigpy#339 (comment) this adds
an `ZCL_POLLING_ATTRS` to define attributes that should be polled
separatelly from the ones that can have reporting config.

This also moves some attributes that do no support reporting config out
of `REPORT_CONFIG`.
@abmantis
Copy link
Contributor Author

Opened a PR with the suggested changes: #354
I'll mark this one as draft until that gets in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
entities Issue or PR about (custom) entities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants